
package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_CONTACTS database table.
 */
@PersistenceCapable( identityType = IdentityType.APPLICATION )
public class Contact extends Base implements Serializable {
	
	private static final long	serialVersionUID	= 1L;
	
	@PrimaryKey
	@Persistent( valueStrategy = IdGeneratorStrategy.IDENTITY )
	private Key					id;
	
	@Persistent
	private String				add1;
	
	@Persistent
	private String				add2;
	
	@Persistent
	private String				city;
	
	@Persistent
	private String				country;
	
	@Persistent
	private long				pin;
	
	@Persistent
	private String				state;
	
	@Persistent
	private Key					createdBy;
	
	@Persistent
	private Date				createdByTime;
	
	@Persistent
	private Key					lastModefiedBy;
	
	@Persistent
	private Date				lastModefiedByTime;
	
	// bi-directional many-to-one association to Email
	@Persistent( mappedBy = "contact" )
	@Element( dependent = "true" )
	private List<Email>			emails				= new ArrayList<Email>();
	
	// bi-directional many-to-one association to Phone
	@Persistent( mappedBy = "contact" )
	@Element( dependent = "true" )
	private List<Phone>			phones				= new ArrayList<Phone>();
	
	public Contact() {

	}
	
	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId( Key id ) {

		this.id = id;
	}
	
	/**
	 * @return the add1
	 */
	public String getAdd1() {

		return add1;
	}
	
	/**
	 * @param add1
	 *            the add1 to set
	 */
	public void setAdd1( String add1 ) {

		this.add1 = add1;
	}
	
	/**
	 * @return the add2
	 */
	public String getAdd2() {

		return add2;
	}
	
	/**
	 * @param add2
	 *            the add2 to set
	 */
	public void setAdd2( String add2 ) {

		this.add2 = add2;
	}
	
	/**
	 * @return the city
	 */
	public String getCity() {

		return city;
	}
	
	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity( String city ) {

		this.city = city;
	}
	
	/**
	 * @return the country
	 */
	public String getCountry() {

		return country;
	}
	
	/**
	 * @param country
	 *            the country to set
	 */
	public void setCountry( String country ) {

		this.country = country;
	}
	
	/**
	 * @return the pin
	 */
	public long getPin() {

		return pin;
	}
	
	/**
	 * @param pin
	 *            the pin to set
	 */
	public void setPin( long pin ) {

		this.pin = pin;
	}
	
	/**
	 * @return the state
	 */
	public String getState() {

		return state;
	}
	
	/**
	 * @param state
	 *            the state to set
	 */
	public void setState( String state ) {

		this.state = state;
	}
	
	/**
	 * @return the emails
	 */
	public List<Email> getEmails() {

		return emails;
	}
	
	/**
	 * @param emails
	 *            the emails to set
	 */
	public void setEmails( List<Email> emails ) {

		this.emails = emails;
	}
	
	/**
	 * @return the phones
	 */
	public List<Phone> getPhones() {

		return phones;
	}
	
	/**
	 * @param phones
	 *            the phones to set
	 */
	public void setPhones( List<Phone> phones ) {

		this.phones = phones;
	}
	
	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.createdBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy( Key createdBy ) {

		this.createdBy = createdBy;
	}
	
	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}
	
	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime( Date createdByTime ) {

		this.createdByTime = createdByTime;
	}
	
	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.lastModefiedBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy( Key lastModefiedBy ) {

		this.lastModefiedBy = lastModefiedBy;
	}
	
	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}
	
	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime( Date lastModefiedByTime ) {

		this.lastModefiedByTime = lastModefiedByTime;
	}
	
	/**
	 * @param contact
	 * @return
	 */
	public Contact importContact( com.pp.admin.common.client.beans.Contact contact ) {

		/**
		 * TODO: add server side validation for contact type.
		 */
		
		this.add1 = contact.getAdd1();
		this.add2 = contact.getAdd2();
		this.city = contact.getCity();
		this.country = contact.getCountry();
		this.state = contact.getState();
		this.pin = contact.getPin();
		
		Iterator<com.pp.admin.common.client.beans.Email> itrEmails = contact.getEmailContacts().iterator();
		com.pp.admin.common.client.beans.Email email = null;
		while ( itrEmails.hasNext() ) {
			email = itrEmails.next();
			this.emails.add( new Email().importEmail( email ) );
		}
		
		Iterator<com.pp.admin.common.client.beans.Phone> itrPhones = contact.getPhoneContacts().iterator();
		com.pp.admin.common.client.beans.Phone phone = null;
		while ( itrPhones.hasNext() ) {
			phone = itrPhones.next();
			this.phones.add( new Phone().importPhone( phone ) );
		}
		
		return this;
	}
	
	public com.pp.admin.common.client.beans.Contact exportContact() {

		com.pp.admin.common.client.beans.Contact contact = new com.pp.admin.common.client.beans.Contact();
		contact.setId( this.id.getId() );
		contact.setAdd1( this.add1 );
		contact.setAdd2( this.add2 );
		contact.setCity( this.city );
		contact.setState( this.state );
		contact.setPin( this.pin );
		contact.setCountry( this.country );
		
		List<com.pp.admin.common.client.beans.Email> emailList = new ArrayList<com.pp.admin.common.client.beans.Email>();
		Email email = null;
		for ( Iterator<Email> iterator = emails.iterator(); iterator.hasNext(); ) {
			email = iterator.next();
			emailList.add( email.exportEmail() );
		}
		contact.setEmailContacts( emailList );
		List<com.pp.admin.common.client.beans.Phone> phoneList = new ArrayList<com.pp.admin.common.client.beans.Phone>();
		Phone phone = null;
		for ( Iterator<Phone> iterator = phones.iterator(); iterator.hasNext(); ) {
			phone = iterator.next();
			phoneList.add( phone.exportPhone() );
		}
		contact.setPhoneContacts( phoneList );
		return contact;
	}
	
	public Contact update( com.pp.admin.common.client.beans.Contact contact ) {

		if ( this.add1 == null || !( this.add1.equalsIgnoreCase( contact.getAdd1() ) ) ) {
			this.add1 = contact.getAdd1();
		}
		if ( this.add2 == null || !( this.add2.equalsIgnoreCase( contact.getAdd2() ) ) ) {
			this.add2 = contact.getAdd2();
		}
		if ( this.city == null || !( this.city.equalsIgnoreCase( contact.getCity() ) ) ) {
			this.city = contact.getCity();
		}
		if ( this.state == null || !( this.state.equalsIgnoreCase( contact.getState() ) ) ) {
			this.state = contact.getState();
		}
		if ( this.pin != contact.getPin() ) {
			this.pin = contact.getPin();
		}
		if ( this.country == null || !( this.country.equalsIgnoreCase( contact.getCountry() ) ) ) {
			this.country = contact.getCountry();
		}
		this.emails.removeAll( emails );
		Iterator<com.pp.admin.common.client.beans.Email> itrEmails = contact.getEmailContacts().iterator();
		com.pp.admin.common.client.beans.Email email = null;
		while ( itrEmails.hasNext() ) {
			email = itrEmails.next();
			this.emails.add( new Email().importEmail( email ) );
		}
		
		this.phones.removeAll( phones );
		Iterator<com.pp.admin.common.client.beans.Phone> itrPhones = contact.getPhoneContacts().iterator();
		com.pp.admin.common.client.beans.Phone phone = null;
		while ( itrPhones.hasNext() ) {
			phone = itrPhones.next();
			this.phones.add( new Phone().importPhone( phone ) );
		}
		return this;
	}
}
